package com.mask.im.business.controller.friend;

import com.mask.im.business.annotation.AesCrypto;
import com.mask.im.business.annotation.AccessBarrier;
import com.mask.im.business.model.vo.FriendDetailVO;
import com.mask.im.business.common.model.PageResult;
import com.mask.im.business.common.model.RestResponse;
import com.mask.im.business.service.FriendshipsService;
import com.mask.im.business.model.dto.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "好友关系模块")
@RestController
public class FriendshipsController {

    @Resource
    private FriendshipsService friendshipsService;


    @ApiOperation("获取所有好友，或者分组后的好友")
    @GetMapping("/users/get/friendships/{type}")
    public RestResponse<List<FriendGroupsSortDTO>> getFriendships(
            @PathVariable String type,
            @RequestParam(value = "remark", required = false) String remark,
            @RequestParam(value = "username", required = false) String username) {
        return friendshipsService.getFriendships(type, remark, username);
    }

    @ApiOperation("获取所有好友，或者分组后的好友")
    @GetMapping("/users/get/friendships1/{type}")
    public RestResponse<List<FriendGroupsSortDTO>> getFriendships1(
            @PathVariable String type,
            @RequestParam(value = "remark", required = false) String remark,
            @RequestParam(value = "username", required = false) String username) {
        return friendshipsService.getFriendships(type, remark, username);
    }

    @ApiOperation("远程实时获取好友（用于搜素用户）")
    @GetMapping("/users/remote/friendships")
    public RestResponse<List<FriendshipsDTO>> remoteFriendships(@RequestParam("keyword") String keyword) {
        return friendshipsService.remoteFriendships(keyword);
    }

    @ApiOperation("获取请求的好友数量")
    @GetMapping("/users/get/applyFriendCount")
    public RestResponse<Integer> applyFriendCount() {
        return friendshipsService.applyFriendCount();
    }


    @ApiOperation("获取所有好友（包括已同意，未同意，拒绝）")
    @GetMapping("/users/get/allFriendRequest")
    public RestResponse<List<FriendshipsDTO>> allFriendRequest() {
        return friendshipsService.allFriendRequest();
    }


    @ApiOperation("搜索需要添加的好友")
    @GetMapping("/users/search/user")
    public RestResponse<?> searchUser(@RequestParam("keyword") String keyword) {
        return friendshipsService.searchUser(keyword);
    }

    @ApiOperation("根据id获取好友状态，查看是否可以添加")
    @GetMapping("/users/getUserById/{friendId}")
    public RestResponse<?> getUserById(@PathVariable Long friendId) {
        return friendshipsService.getUserById(friendId);
    }

    @ApiOperation("加入黑名单")
    @PutMapping("/users/addBlacklist/{friendId}")
    public RestResponse<String> addBlacklist(@PathVariable Long friendId) {
        return friendshipsService.addBlacklist(friendId);
    }

    @ApiOperation("更新星标好友")
    @PutMapping("/users/updateStar")
    public RestResponse<String> updateStar(@RequestParam("twoUserId") Long twoUserId,
                                           @RequestParam("isStar") Integer isStar) {
        return friendshipsService.updateStar(twoUserId, isStar);
    }

    @ApiOperation("删除好友")
    @DeleteMapping("/users/delete/friend")
    public RestResponse<String> deleteFriend(@RequestParam("friendId") Long friendId) {
        friendshipsService.deleteFriend(friendId);
        return RestResponse.ok();
    }

    @ApiOperation("根据好友id获取好友信息")
    @GetMapping("/users/getFriendById")
    public RestResponse<FriendDetailDTO> getFriendById(@RequestParam("friendId") Long friendId) {
        return friendshipsService.getFriendById(friendId);
    }


    @ApiOperation("获取好友动态变化的位置索引")
    @GetMapping("/users/getInsertLocation")
    public RestResponse<Integer> getInsertLocation() {
        return friendshipsService.getInsertLocation();
    }

    @ApiOperation("修改好友备注标签描述等")
    @PostMapping("/users/updateFriendDetail")
    @AesCrypto
    public RestResponse<FriendDetailDTO> updateFriendDetail(@RequestBody FriendDetailVO friendDetailVO) {
        return friendshipsService.updateFriendDetail(friendDetailVO);
    }


    @ApiOperation("获取黑名单好友")
    @GetMapping("/users/getBlacklistFriend")
    public PageResult<FriendsBlacklistDTO> getBlacklistFriend(@RequestParam(value = "remark", defaultValue = "") String remark) {
        return friendshipsService.getBlacklistFriend(remark);
    }

    @ApiOperation("移出好友黑免单")
    @DeleteMapping("/users/cancelBlacklistFriend/{friendIds}")
    public RestResponse<String> cancelBlacklistFriend(@PathVariable String friendIds) {
        friendshipsService.cancelBlacklistFriend(friendIds);
        return RestResponse.ok();
    }

    @ApiOperation("改变未读数量状态")
    @PutMapping("/users/addUnreadCount/{friendId}")
    public RestResponse<String> addUnreadCount(@PathVariable Long friendId) {
        friendshipsService.addUnreadCount(friendId);
        return RestResponse.ok();
    }

    @ApiOperation("清空未读")
    @PutMapping("/users/clearUnreadCount/{friendId}")
    public RestResponse<String> clearUnreadCount(@PathVariable Long friendId) {
        friendshipsService.clearUnreadCount(friendId);
        return RestResponse.ok();
    }

    @ApiOperation("获取侧边栏右键的状态信息")
    @GetMapping("/users/getSidewaysStatus")
    public RestResponse<SidewaysStatusDTO> getSidewaysStatus(@RequestParam("friendId") Long friendId) {
        return friendshipsService.getSidewaysStatus(friendId);
    }

    @ApiOperation("置顶或取消置顶")
    @PutMapping("/users/isTop/{status}")
    public RestResponse<String> isTop(@PathVariable Integer status, @RequestParam("friendId") Long friendId) {
        friendshipsService.isTop(friendId, status);
        return RestResponse.ok();
    }

    @ApiOperation("拒绝添加好友")
    @DeleteMapping("/users/reject")
    public RestResponse<String> rejectAddFriend(@RequestParam("friendId") Long friendId) {
        friendshipsService.rejectAddFriend(friendId);
        return RestResponse.ok();
    }

    @ApiOperation("通知好友上线")
    @AccessBarrier(limitTime = 60 * 60 * 24)
    @PostMapping("/users/notify/friend/online")
    @AesCrypto
    public RestResponse<String> notifyFriendOnline(@RequestBody NotifyFriend notifyFriend) {
        friendshipsService.notifyFriendOnline(notifyFriend);
        return RestResponse.ok();
    }

    @ApiOperation("获取所有好友")
    @GetMapping("/users/getAllFriend")
    public RestResponse<List<GroupChatShowDTO>> buildGroupChatShow(@RequestParam(value = "keyword", defaultValue = "") String keyword) {
        return friendshipsService.buildGroupChatShow(keyword);
    }

    @ApiOperation("获取除要推荐好友外的所有好友")
    @GetMapping("/users/getRecommendFriends")
    public RestResponse<List<PersonalDTO>> recommendFriends(
            @RequestParam("userInfoId") Long userInfoId,
            @RequestParam(value = "keyword", defaultValue = "") String keyword) {
        return friendshipsService.getRecommendFriends(userInfoId, keyword);
    }

    //TODO 未加入接口
    @ApiOperation("获取转账时好友信息")
    @GetMapping("/users/transfer/message")
    public RestResponse<TransferMessageDTO> transferMessage(@RequestParam("friendId") Long friendId) {
        return friendshipsService.transferMessage(friendId);
    }
}

